export const ctime = (nowtime) => {
  let oDate = new Date(nowtime),
    oYear = oDate.getFullYear(),
    oMonth = oDate.getMonth() + 1,
    oDay = oDate.getDate(),
    oHour = oDate.getHours(),
    oMin = oDate.getMinutes(),
    oSen = oDate.getSeconds(),
    oTime = oYear + '-' + oMonth + '-' + oDay + ' ' + oHour + ':' + oMin + ':' + oSen; //最后拼接时间
  return oTime;
}

export const format = (timeStamp, fmt="yyyy-MM-dd") => {
  let data = new Date(timeStamp)
  let o = {
    "M+": data.getMonth() + 1, //月份
    "d+": data.getDate(), //日
    "h+": data.getHours(), //小时
    "m+": data.getMinutes(), //分
    "s+": data.getSeconds(), //秒
    "q+": Math.floor((data.getMonth() + 3) / 3), //季度
    "S": data.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (data.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  // console.log(format(1547019710960))
  return fmt;

}
  //特殊字符验证
  export const formatInput = (val) => {
    var reg = new RegExp("[` ~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ % + -]");
    var rs = "";
    for (var i = 0, l = val.length; i < val.length; i++) {
      rs = rs + val.substr(i, 1).replace(reg, '');
    }
    return rs;
  }
// 去除空格
//权限列表遍历
export const has = (functionId, vue) => {
  let authIdList = vue.$store.getters.getAuthList;
  if (authIdList.indexOf(functionId) > -1) {
    return true
  }
}

// 导出excel
// export const exportToExcel = (header, filter, lists, title) => {
//   require.ensure([], () => {
//     const {
//       export_json_to_excel
//     } = require('@/vendor/Export2Excel')
//     const tHeader = header
//     const filterVal = filter
//     const list = lists
//     const data = formatJson(filterVal, list)
//     export_json_to_excel(tHeader, data, title)
//     //delete this.list[this.list.length]
//   })
// }
// 解析json
function formatJson(filterVal, jsonData) {
  return jsonData.map(v => filterVal.map(j => v[j]))
}
// excel转json

export const importToJson = (data) => {
  var wb; //读取完成的数据
  var rABS = false; //是否将文件读取为二进制字符串
  wb = XLSX.read(data, {
    type: 'binary'
  });

  return XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])

}

// 通过年月判断改月的总天数
export const getDays = (month, year) => {
  month = parseInt(month, 10);
  var temp = new Date(year, month, 0);
  return temp.getDate();
}

// 通过年月输出日期（月天数以及星期）列表
export const getMonthDay = (date) => {
  let day = new Date().getDate();
  let dateArray = date.split('-')
  let month = parseInt(dateArray[1]);
  let year = parseInt(dateArray[0]);
  let days = getDays(month, year); // 获取总天数
  let callBackDataList = [];
  for (let i = 1; i <= days; i++) {
    //callBackDataList.push(`${month}/${i}`);
    let xq = getXq(`${date}-${i}`);
    if (i == day) {
      callBackDataList.push({
        rq: {
          rq: `${i}`,
          isToday: true
        },
        xq: {
          xq: xq,
          isToday: true
        }
      });
    } else if (xq == "六" || xq == "天") {
      callBackDataList.push({
        rq: {
          rq: `${i}`,
          isWeekend: true
        },
        xq: {
          xq: xq,
          isWeekend: true
        }
      });
    } else {
      callBackDataList.push({
        rq: {
          rq: `${i}`
        },
        xq: {
          xq: xq
        }
      });
    }


  }
  return callBackDataList
}

// 通过日期判断星期
export const getXq = (date) => {
  return '天一二三四五六'.charAt(new Date(date).getDay())
}
